﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web.UI.HtmlControls;
using System.Web.UI;

using DevExpress.Web.ASPxHeadline;

using LMD.Lib.DataClass;
using LMD.Constant;
using LMD.Lib.Controls;
using LMD.Lib.Oracle;

/// <summary>
/// Summary description for Extension
/// </summary>
public static class CompanyProfileManager
{
    public static String GetBriefDesc(this CompanyProfileData cpData)
    {
        Cached.CompanyTable ccCompany = new Cached.CompanyTable();
        DataTable dt = ccCompany.GetCompanyBriefDesc(cpData.ID, true);

        String sxOutput = String.Empty;
        
        if (dt.Rows.Count > 0)
        {
            sxOutput = Convert.ToString(dt.Rows[0]["VALUE"]);
        }

        return sxOutput;
    }

    public static String BuildCompanyItem(this CompanyProfileData cpData)
    {
        String sxOutput = String.Format(
            "<table cellspacing=\"0\" cellpadding=\"0\" id=\"{1}_{0}\" border=\"0\" class=\"cpT\">" +
                "<tr>" +
                    "<td valign=\"top\" style=\"width:50px;\">" +
                        "<a href=\"CompanyProfile.aspx?cid={0}\"><img src=\"upload/company/{0}.jpg\" onerror=\"setEmptyCompanyAvatar(this);\" alt=\"\" class=\"cpP\" /></a>" +
                    "</td>" +
                    "<td valign=\"top\" style=\"padding-left:12px;\">" +
                        "<a class=\"cpH\" href=\"CompanyProfile.aspx?cid={0}\">{2}</a>" +
                        "<br/><span style=\"font-size:8pt;\">CAR: </span><a class=\"lnkless\" href=\"NewsFeed.aspx?uid={3}&mode=profile\">{4}</a>" +
                        "<br/><span style=\"font-size:8pt;\">Sales: </span><a class=\"lnkless\" href=\"NewsFeed.aspx?uid={5}&mode=profile\">{6}</a>" +
                    "</td>" +
                "</tr>" +
            "</table>",
            cpData.ID,
            KCompanyProfile.DIV_ID,
            cpData.sxCompanyName,
            cpData.sxCAREmployeeNo,
            cpData.sxCARName,
            cpData.sxSalesEmployeeNo,
            cpData.sxSalesName);

        return sxOutput;
    }
    public static String BuildEditor_BriefDesc(this CompanyProfileData cpData)
    {
        String sxOutput = String.Format(
            "<textarea class=\"m_taFw\" name=\"{0}\" id=\"{0}\">{1}</textarea>",
            KControl.TEXTAREA_ID,
            cpData.GetBriefDesc());

        return sxOutput;
    }
    public static String BuildFieldSet_BriefDesc(this CompanyProfileData cpData)
    {
        String sxOutput = cpData.GetBriefDesc();

        return sxOutput;
    }

    public static String BuildEditor_BasicInfo(this CompanyProfileData cpData)
    {
        Cached.CustomerInfoTypeTable ccCustInfo = new Cached.CustomerInfoTypeTable();
        DataTable dtCustInfo = null;

        // NEXT THIS SHOULD LOOP FROM A ACOLLECTION, RETRIEVED FROM DATABASE
        String sxOutput = String.Empty;
        sxOutput += CustomControls.BuildFieldSet_PairedLabel("Name", cpData.sxCompanyName);
        sxOutput += CustomControls.BuildFieldSet_PairedLabel("Address", cpData.sxFullAddress);
        sxOutput += CustomControls.BuildFieldSet_PairedLabel("Business Segment", cpData.sxBusinessSegment);
        sxOutput += CustomControls.BuildFieldSet_PairedLabel("Customer Class", cpData.sxClass);
        sxOutput += CustomControls.BuildFieldSet_PairedLabel("Company Segment", cpData.sxCompanySegment);
        sxOutput += CustomControls.BuildFieldSet_PairedLabel("NPWP", cpData.sxNPWP);


        var cpDic = cpData.GetCustomDataDictionary();

        if (cpDic.Count < 1)
        {
            cpDic.Add(String.Empty, String.Empty);
        }
        else
        {
            dtCustInfo = ccCustInfo.GetCustomerInfoType();
        }

        sxOutput += CustomControls.BuildEditor_ComboBoxAndText(cpData.ID.ToString(),
            KLOV.CompanyProfile.BASICINFOTYPE, KControl.INPUT_ID, dtCustInfo, "ID", "CODE", cpDic);
        sxOutput += CustomControls.Create_HiddenDiv(KCompanyProfile.HF_BASICINFOCOUNT,
            cpDic.Count.ToString());
    
        return sxOutput;
    }

    public static String BuildFieldSet_BasicInfo(this CompanyProfileData cpData)
    {
        

        // NEXT THIS SHOULD LOOP FROM A ACOLLECTION, RETRIEVED FROM DATABASE
        String sxOutput = String.Empty;
        sxOutput += CustomControls.BuildFieldSet_PairedLabel("Name", cpData.sxCompanyName);
        sxOutput += CustomControls.BuildFieldSet_PairedLabel("Address", cpData.sxFullAddress);
        sxOutput += CustomControls.BuildFieldSet_PairedLabel("Business Segment", cpData.sxBusinessSegment);
        sxOutput += CustomControls.BuildFieldSet_PairedLabel("Customer Class", cpData.sxClass);
        sxOutput += CustomControls.BuildFieldSet_PairedLabel("Company Segment", cpData.sxCompanySegment);
        sxOutput += CustomControls.BuildFieldSet_PairedLabel("NPWP", cpData.sxNPWP);

         var cpDic = cpData.GetCustomDataDictionary();

         if (cpDic != null)
         {
             Cached.CustomerInfoTypeTable ccCustInfo = new Cached.CustomerInfoTypeTable();
             DataTable dtCustInfo = ccCustInfo.GetCustomerInfoType();

             //sxOutput += BuildFieldSet_BasicInfo(dtCustInfo, "ID", "CODE", cpDic);
             sxOutput += CustomControls.BuildFieldSet_PairedInfo(dtCustInfo, "ID", "CODE", "CONTROL_TYPE_ID", cpDic);
         }

         //sxOutput += String.Format("<br/><p>{0}</p>", cpData.sxSummary);

        return sxOutput;
    }

    public static String BuildEditor_ContactInfo(this ContactInfoData ciData)
    {
        // NEXT THIS ACOLLECTION SHOULD BE RETRIEVED FROM DATABASE
        //Dictionary<String, String> dicOptions = FakeDataClass.CP_GetContactInfoList();
        Cached.ContactDataTypeTable ccContactData = new Cached.ContactDataTypeTable();
        DataTable dtContactData = ccContactData.GetContactDataType();

        // NEXT THIS SHOULD LOOP FROM A ACOLLECTION, RETRIEVED FROM DATABASE
        String sxOutput = String.Empty;
        
        //sxOutput += CustomControls.BuildFieldSet_PairedLabel(String.Empty, null);
        sxOutput += CustomControls.BuildEditor_LabelAndText(KCompanyProfile.NAMA_KONTAK, KControl.INPUT_ID, 
            CompanyProfileManager.ConvertToLabel(KCompanyProfile.NAMA_KONTAK), ciData.sxContactName, TextboxType.FreeText, true);
        
        Dictionary<String,String> dicGender = new Dictionary<String,String>(); 
        dicGender.Add(((int)Gender.Male).ToString(), Gender.Male.GetString());
        dicGender.Add(((int)Gender.Female).ToString(), Gender.Female.GetString());

        String sxCombo = CustomControls.Create_ComboBox(KCompanyProfile.GENDER_KONTAK, KControl.INPUT_ID, 
            dicGender, ((int)ciData.Sex).ToString());
 
        // Checkbox is only needed to bw shown when adding new CP or edit existing CP
        String sxRadio = ciData.ID < 0 || !ciData.IsNewCP ?
            CustomControls.Create_CheckBox(KCompanyProfile.CP_TEKNIS, KControl.INPUT_ID, "Contact Person Teknis", ciData.IsCPTeknis) :
            String.Empty;

        sxOutput += CustomControls.BuildFieldSet_PairedLabel(
            CompanyProfileManager.ConvertToLabel(KCompanyProfile.GENDER_KONTAK), sxCombo + "  " + sxRadio);

        String sxJobLOV = CustomControls.Create_OTFComboBox(KCompanyProfile.JABATAN_KONTAK, 
            KLOV.Generic.PICPOSITION, ciData.sxJobPositionName, ciData.JobPositionID.ToString(), 
            TextboxType.Empty, String.Empty, "SaveSelectedPositionId()", true);

        sxOutput += CustomControls.BuildFieldSet_PairedLabel(
            CompanyProfileManager.ConvertToLabel(KCompanyProfile.JABATAN_KONTAK), sxJobLOV);

        // create hidden field for getting data by loop purposes
        sxOutput += CustomControls.Create_TextBox("hf_" + KCompanyProfile.JABATAN_KONTAK,
            KControl.INPUT_ID, ciData.JobPositionID.ToString(), true, TextboxType.Empty,true);

        sxOutput += CustomControls.BuildEditor_LabelAndText(KCompanyProfile.TELEPON_KONTAK, KControl.INPUT_ID,
            CompanyProfileManager.ConvertToLabel(KCompanyProfile.TELEPON_KONTAK), ciData.sxPhoneNumber, TextboxType.PhoneNumber,true);
        sxOutput += CustomControls.BuildEditor_LabelAndText(KCompanyProfile.PONSEL_KONTAK, KControl.INPUT_ID, 
            CompanyProfileManager.ConvertToLabel(KCompanyProfile.PONSEL_KONTAK), ciData.sxCellPhoneNumber, TextboxType.PhoneNumber,true);
        sxOutput += CustomControls.BuildEditor_LabelAndText(KCompanyProfile.EMAIL_KONTAK, KControl.INPUT_ID, 
            CompanyProfileManager.ConvertToLabel(KCompanyProfile.EMAIL_KONTAK), ciData.sxEmailAddress, TextboxType.Email,true);

        var ciDic = ciData.GetCustomDataDictionary();

        if (ciDic.Count < 1)
        {
            ciDic.Add(String.Empty, String.Empty);
        }
        else
        {
            dtContactData = ccContactData.GetContactDataType();
        }

        sxOutput += CustomControls.BuildEditor_ComboBoxAndText(ciData.ID.ToString(),
            KLOV.CompanyProfile.CONTACTINFOTYPE, KControl.INPUT_ID, dtContactData, "ID", "CODE", ciDic);
        sxOutput += CustomControls.Create_HiddenDiv(KCompanyProfile.HF_CONTACTINFOCOUNT,
            ciDic.Count.ToString());
        
        //sxOutput += CustomControls.BuildEditor_ComboBoxAndText("ci_" + ciData.ID.ToString(), 
        //    KLOV.CompanyProfile.CONTACTINFOTYPE, KControl.INPUT_ID, dicOptions, ciData.GetCustomDataDictionary());
        //sxOutput += CustomControls.Create_HiddenDiv(KCompanyProfile.HF_CONTACTINFOCOUNT,
        //    ciData.GetCustomDataDictionary().Count.ToString());

        String sxLink = CustomControls.Create_LinkDiv(KCompanyProfile.LINK_ADDCONTACTINFO,
            String.Format("AddInfo2Contact(this, '{0}');", ciData.ID.ToString()), "add info++");

        sxOutput += CustomControls.BuildFieldSet_PairedLabel(sxLink, null);

        return sxOutput;
    }

    public static String BuildFieldSet_ContactInfo(this ContactInfoData ciData)
    {
        // NEXT THIS ACOLLECTION SHOULD BE RETRIEVED FROM DATABASE
        //Dictionary<String, String> dicOptions = FakeDataClass.CP_GetContactInfoList();
        Cached.ContactDataTypeTable ccContactData = new Cached.ContactDataTypeTable();
        DataTable dtContactData = ccContactData.GetContactDataType();

        String sxLOV = CustomControls.Create_OTFComboBox("cp_" + ciData.CompanyID, KLOV.Generic.CONTACTPERSON, 
            ciData.sxContactName, ciData.ID.ToString(), TextboxType.Empty, ciData.CompanyID.ToString(), 
            "ShowSelectedContactData()");
        
        // NEXT THIS SHOULD LOOP FROM A ACOLLECTION, RETRIEVED FROM DATABASE
        String sxOutput = String.Empty;
        sxOutput += CustomControls.BuildFieldSet_PairedLabel("<B>Nama</B>", sxLOV);
        sxOutput += CustomControls.BuildFieldSet_PairedLabel("Jenis Kelamin", ciData.Sex.GetString());
        sxOutput += CustomControls.BuildFieldSet_PairedLabel("Jabatan", ciData.sxJobPositionName);
        sxOutput += CustomControls.BuildFieldSet_PairedLabel("Nomer Telepon", ciData.sxPhoneNumber);
        sxOutput += CustomControls.BuildFieldSet_PairedLabel("Nomer HP", ciData.sxCellPhoneNumber);
        sxOutput += CustomControls.BuildFieldSet_PairedLabel("Email", ciData.sxEmailAddress);

        if (ciData.IsCPTeknis)
        {
            sxOutput += CustomControls.BuildFieldSet_PairedLabel("Jenis", "Kontak Person Teknis");
        }

        var ciDic = ciData.GetCustomDataDictionary();

        if (ciDic != null)
        {
            //sxOutput += BuildFieldSet_BasicInfo(dtCustInfo, "ID", "CODE", cpDic);
            sxOutput += CustomControls.BuildFieldSet_PairedInfo(dtContactData, "ID", "CODE", "CONTROL_TYPE_ID", ciDic);
        }

        return sxOutput;
    }

    

    public static String ConvertToLabel(String sxInput)
    {
        switch (sxInput)
        {
            case KCompanyProfile.NAMA_PERUSAHAAN: return "Nama Perusahaan";
            case KCompanyProfile.ALAMAT_LENGKAP: return "Alamat Lengkap";
            case KCompanyProfile.BIDANG_USAHA: return "Bidang Usaha";

            case KCompanyProfile.NAMA_KONTAK: return "Nama";
            case KCompanyProfile.GENDER_KONTAK: return "Jenis Kelamin";
            case KCompanyProfile.EMAIL_KONTAK: return "Email";
            case KCompanyProfile.TELEPON_KONTAK: return "Nomer Telepon";
            case KCompanyProfile.JABATAN_KONTAK: return "Jabatan";
            case KCompanyProfile.PONSEL_KONTAK: return "Nomer HP";
            default: return sxInput;
        }
    }

    public static string BuildEditor_CompanyLogoPopup(string sxCompanyId)
    {
        String sxReturn = String.Empty;



        String sxUploadControl = CustomControls.BuildEditor_FileUpload(sxCompanyId,
            KFileHandler.CompanyProfile.COMPANY_LOGO, null, KControl.INPUT_ID, TextboxType.ImageUpload);

        String sxEditor = CustomControls.BuildFieldSet_PairedLabel("Company Logo", sxUploadControl);

        sxReturn = String.Format(
            "<div id=\"{1}_{0}\" class=\"popLOV\">" +
                "<fieldset class=\"m_fs_border\" id=\fs_lov_{0}\">" +
                    "<legend>" +
                        "<a class=\"lnk\" href=\"javascript:toggle('{0}');\">Upload Company Logo</a>" +
                    "</legend>" +
                    "<dl class=\"m_fs\"><div id=\"div_{0}\">{2}</div></dl>" +
                    "<div id=\"load_{0}\" style=\"float: right; padding-right: 10px;\"></div>" +
                    "<br/>" +
                    "<br/>" +
                    "<input type=\"button\" id=\"lnkdscd_{0}\" class=\"btn\" style=\"float: right; margin-left: 5px; margin-right: 15px;\"" +
                        " onclick=\"closeEditor();\" value=\"close\" />" +
                "</fieldset>" +
            "</div>",
            sxCompanyId,
            KControl.GENERICPOPFORM_ID,
            sxEditor
            );
        return sxReturn;
    }
        
}